<?php
/**
 * 后台主菜单菜单Widgets
 * Created by PhpStorm.
 * User: crstu
 * Date: 2016/5/25
 * Time: 11:13
 */

namespace backend\widgets;


use yii\base\Widget;
use yii\helpers\Url;
use backend\models\Service\AdminService;


class MainMenu extends Widget
{

    /**
     * 传入的菜单数据，可以默认为空
     * @array
     */
    public $data = [];

    public function init()
    {

        //根据权限初始化菜单数据
        if(empty($this->data)){
            $role_id = \Yii::$app->session->get('ADMIN_ROLE_ID');
            $this->data = AdminService::initPower($role_id);
        }

        if(is_array($this->data) && !empty($this->data)){
            foreach ($this->data as $parent_k => $parent_menu){
                if($parent_menu['display']){
                    echo '<li class="nav-item ">';
                    echo '<a href="javascript:;" class="nav-link nav-toggle">';
                    echo '<i class="'.$parent_menu['icon'].'"></i>';
                    echo '<span>'.$parent_menu['title'].'</span>';
                    echo '<span class="arrow"></span>';
                    echo '</a>';

                    if(!empty($parent_menu['child'])){

                        echo '<ul class="sub-menu">';

                        foreach ($parent_menu['child'] as $son_k => $son){
                            if(!empty($son[3])){

                                echo '<li class="nav-item">';

                                if(isset($son[4]) && is_array($son[4])){
                                    $url = Url::to([$son[0].'/'.$son[1]]);
                                    echo '<a href="'.$url.'" class="nav-link nav-toggle"><span>'.$son['2'].'</span><span class="arrow"></span></a>';
                                    echo '<ul class="sub-menu">';
                                    foreach ($son[4] as $ss){
                                        if($ss[3]){
                                            $url = Url::to([$ss[0].'/'.$ss[1]]);
                                            echo '<li class="nav-item "><a href="'.$url.'" class="nav-link">'.$ss['2'].'</a></li>';
                                        }
                                    }
                                    echo '</ul>';
                                }else{
                                    $url = Url::to([$son[0].'/'.$son[1]]);
                                    echo '<li class="nav-item"><a href="'.$url.'" class="nav-link">'.$son['2'].'</a></li>';
                                }

                                echo '</li>';
                            }
                        }

                        echo '</ul>';
                    }

                    echo '</li>';
                }
            }
        }
    }
}